// MEDIC

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

ENT.iOwner = nil
ENT.HitSound = Sound("buttons/button24.wav")

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "medic_ball" )
		ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end

function ENT:Initialize()

	self.Entity:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
	
	self.Entity:PhysicsInitSphere( 13, "metal_bouncy" )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
	
	self.Entity:SetCollisionBounds( Vector( -13, -13, -13 ), Vector( 13, 13, 13 ) )
	
	util.SpriteTrail( self.Entity, 0, Color( 255, 120, 120), true, 20, 0, 0.7, 1/(18+1)*0.5, "trails/plasma.vmt" )
	
	timer.Create("Tmedic"..tostring(math.Rand(0,15000))..tostring(CurTime()/10),5,0, function()
		if ValidEntity(self.Entity) then
			local vPoint = self.Entity:GetPos() + Vector(0,0,1)
			local effectdata = EffectData()
			effectdata:SetOrigin( vPoint )
			effectdata:SetScale( 1 )
			util.Effect( "ball_medic", effectdata )
			self.Entity:EmitSound( "buttons/button24.wav" )
		end	
	end)
	
	timer.Create("T"..tostring(math.Rand(0,10000))..tostring(CurTime()/10),20,1, function()
		if ValidEntity(self.Entity) then
			local vPoint = self.Entity:GetPos() + Vector(0,0,1)
			local effectdata = EffectData()
			effectdata:SetOrigin( vPoint )
			effectdata:SetScale( 1 )
			util.Effect( "ball_medic", effectdata )
			self.Entity:Remove()
		end	
	end)
end

function ENT:PhysicsCollide( data, physobj )
	
	if (data.Speed > 80 && data.DeltaTime > 0.2 ) then
		self.Entity:EmitSound( "buttons/button24.wav" )
	end
	
	local LastSpeed = math.max( data.OurOldVelocity:Length(), data.Speed )
	local NewVelocity = physobj:GetVelocity()
	NewVelocity:Normalize()
	
	LastSpeed = math.max( NewVelocity:Length(), LastSpeed )
	
	local TargetVelocity = NewVelocity * LastSpeed * 0.2
	
	physobj:SetVelocity( TargetVelocity )
	
end

function ENT:OnTakeDamage( dmginfo )

	self.Entity:TakePhysicsDamage( dmginfo )
	
end


function ENT:Touch( entidadeTocada )
	if !ValidEntity(entidadeTocada) then return end
	if !entidadeTocada:IsPlayer() then return end
	if entidadeTocada:Team() != self.Entity.iOwner:Team() then return end
	
	entidadeTocada:EmitSound(self.Entity.HitSound,200,100)

	local cura = math.random( 10, 20 )
	if entidadeTocada:Health() + cura < 100 then
	
		entidadeTocada:SetHealth( entidadeTocada:Health() + cura )
		
		entidadeTocada:PrintMessage( HUD_PRINTCENTER, "Healed!")
	
	end
	
	if ValidEntity(self.Entity) then
		local vPoint = self.Entity:GetPos() + Vector(0,0,1)
		local effectdata = EffectData()
		effectdata:SetOrigin( vPoint )
		effectdata:SetScale( 0.7 )
		util.Effect( "ball_medic", effectdata )
		self.Entity:Remove()
	end	

end



